﻿#pragma execution_character_set("utf-8")

#include "page_deviceList.h"
#include "ui_page_deviceList.h"
#include "page_defaultDevList.h"
#include "page_myDevList.h"
#include "page_rollDevList.h"
#include <QResizeEvent>

deviceListPage::deviceListPage(QWidget *parent) : QWidget(parent), ui(new Ui::deviceListPage)
{
    ui->setupUi(this);
    this->initForm();
}

deviceListPage::~deviceListPage()
{
    delete ui;
}

void deviceListPage::initForm()
{
    defaultDevListPage *page1 = new defaultDevListPage(this);
    ui->stackedWidget->addWidget(page1);
    myDevListPage *page2 = new myDevListPage(this);
    ui->stackedWidget->addWidget(page2);
    rollDevListPage *page3 = new rollDevListPage(this);
    ui->stackedWidget->addWidget(page3);

    QList<QAbstractButton *> tbtns = ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setCheckable(true);
        connect(btn, SIGNAL(clicked()), this, SLOT(buttonClick()));
    }

    ui->widgetTop->setProperty("nav", "top");
    ui->btn_default_devlist->click();
}

void deviceListPage::buttonClick()
{
    QAbstractButton *b = (QAbstractButton *)sender();
    QString name = b->objectName();

    QList<QAbstractButton *> tbtns = ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setChecked(btn == b);
    }

    if (name == "btn_default_devlist") {
        ui->stackedWidget->setCurrentIndex(0);
    } else if (name == "btn_my_devlist") {
        ui->stackedWidget->setCurrentIndex(1);
    } else if (name == "btn_roll_devlist") {
        ui->stackedWidget->setCurrentIndex(2);
    }
}

void deviceListPage::resizeEvent(QResizeEvent *ev)
{
    QWidget::resizeEvent(ev);
    if (ev->oldSize() != ev->size())
    {
        Q_EMIT resize_x();
    }
}
